import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'

class LTRequest {
  instance: AxiosInstance
  constructor(baseUrl: string, timeout = 5000) {
    // 创建axios实例
    this.instance = axios.create({
      baseURL: baseUrl,
      timeout: timeout
    })
    // 请求拦截器
    this.instance.interceptors.request.use(
      (config) => {
        return config
      },
      (err) => {
        return Promise.reject(err)
      }
    )
    // 响应拦截器
    this.instance.interceptors.response.use(
      (res) => {
        return res.data
      },
      (err) => {
        return Promise.reject(err)
      }
    )
  }
  request(config: AxiosRequestConfig) {
    return this.instance.request(config)
  }
  get(config: AxiosRequestConfig) {
    return this.request(config)
  }
  post(config: AxiosRequestConfig) {
    return this.request(config)
  }
}

export default LTRequest
